/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于smart-plant-iot系统.
 */
package com.webide.setting.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.webide.core.base.BaseDAO;
import com.webide.core.base.BaseSVImpl;
import com.webide.core.entity.Page;
import com.webide.core.exceptions.BaseException;
import com.webide.core.exceptions.SettingException;
import com.webide.setting.dao.SettingDAO;
import com.webide.setting.entity.Setting;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("settingSV")
@Slf4j
public class SettingSVImpl extends BaseSVImpl<Setting, Long> implements SettingSV {

    @Autowired
    private SettingDAO settingDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return settingDAO;
    }

    /**
    * 保存account对象
    *
    * @param entity 实体
    * @throws BaseException
    */
    @Override
    public void save(Setting entity) throws BaseException {
        super.save(entity);
    }

    /**
     * 加载一个对象Setting
     * @param id 
     * @return Setting
     */
    @Override
    public Setting load(Long id) {
           if(id==null){
                 throw new SettingException(BaseException.BaseExceptionEnum.Ilegal_Param);
           }

            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            return settingDAO.load(param);
    }
    /**
     * 加载一个对象Setting 通过id
     * @param id
     * @return Setting
     */
    @Override
    public Setting loadById(Long id) {
            if(id==null){
                 throw new SettingException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            return settingDAO.loadById(id);
    }


    /**
     * 删除对象Setting
     * @param id
     * @return Setting
     */
    @Override
    public void delete(Long id) {
            if(id==null){
                throw new SettingException(BaseException.BaseExceptionEnum.Ilegal_Param);
            }
            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            settingDAO.delete(param);
    }



    /**
    * 查询Setting分页
    *
    * @param setting  对象
    * @param offset 查询开始行
    * @param limit  查询行数
    * @return List<Setting>
    */
    @Override
    public List<Setting> list(Setting setting, int offset, int limit) {
            if (offset < 0) {
               offset = 0;
            }

            if (limit < 0) {
               limit = Page.limit;
            }

            Map<String, Object> map = null;
            if (setting != null) {
               map = JSON.parseObject(JSON.toJSONString(setting, SerializerFeature.WriteDateUseDateFormat));
            } else {
               map = new HashMap<>();
            }
        return settingDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public  int count(Setting setting){
        Map<String, Object> map = null;
        if (setting != null) {
           map = JSON.parseObject(JSON.toJSONString(setting, SerializerFeature.WriteDateUseDateFormat));
        } else {
           map = new HashMap<>();
        }
       return settingDAO.count(map);
     }

    /**
     * 查询Setting分页
     *
     * @param id
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<Setting>
     */
    @Override
    public List<Setting> list(Long id,int offset, int limit) {
            if (offset < 0) {
              offset = 0;
            }

            if (limit < 0) {
              limit = Page.limit;
            }
            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            return settingDAO.list(param,new RowBounds(offset, limit));
    }

    @Override
    public int count(Long id) {
            Map<String,Object> param=new HashMap<>();
            param.put("id",id);
            return settingDAO.count(param);
    }

    @Override
    public Setting loadByK(String k) {
        return settingDAO.loadByK(k);
    }

}
